package com.yufei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yufei.dao.UserDao;
import com.yufei.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
// 允许跨域
@CrossOrigin
public class UserController {
    @Autowired
    private UserDao userDao;

    //查询所有
    @GetMapping("/user")
    public List<User> getAll() {
        return userDao.selectList(null);
    }

    // 分页查询
    @GetMapping("/page/{current}")
    public IPage<User> pageSelect(@PathVariable Integer current) {
        IPage page = new Page(current, 12);
        userDao.selectPage(page, null);
        return page;
    }

    //新增人员
    @PostMapping("/add")
    public String save(@RequestBody User user) {
        userDao.insert(user);
        return "OK";
    }

    //修改
    @PostMapping("/update")
    public String update(@RequestBody User user) {
        userDao.updateById(user);
        return "OK";
    }

    //删除接口
    @PostMapping("/delete/{id}")
    public int delete(@PathVariable Integer id) {
        return userDao.deleteById(id);
    }

    // 条件查询
    @GetMapping("/selectName/{name}")
    public List selectName(@PathVariable String name) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.like("name", name);
        return userDao.selectList(qw);
    }

}
